{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pywinauto\n",
    "from pywinauto import clipboard\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pywinauto\\application.py:1032: UserWarning: 32-bit application should be automated using 32-bit Python (you use 64-bit Python)\n",
      "  UserWarning)\n"
     ]
    }
   ],
   "source": [
    "app = pywinauto.Application().connect(path=r\"C:\\同花顺软件\\同花顺\\xiadan.exe\",timeout=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "main = app.top_window()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_left_menus_handle():\n",
    "    while True:\n",
    "        try:\n",
    "            handle = main.window(\n",
    "                control_id=129,\n",
    "                class_name='SysTreeView32'\n",
    "            )\n",
    "            # sometime can't find handle ready, must retry\n",
    "            handle.wait('ready', 2)\n",
    "            return handle\n",
    "        except:\n",
    "            pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 查询持有股票"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pywinauto.controls.common_controls._treeview_element at 0x191c3546f60>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_left_menus_handle().get_item(['查询[F4]', '资金股票']).click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from captcha_recognize import captcha_recognize\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "captcha--> 6797\n",
      "明细\t证券代码\t证券名称\t股票余额\t可用余额\t冻结数量\t盈亏\t成本价\t盈亏比例(%)\t市价\t市值\t交易市场\t股东帐户\t实际数量\t可申赎数量\t资讯\t\n",
      "\t000333\t美的集团\t200\t200\t0\t5623.08\t18.985\t148.10\t47.10\t9420.00\t深圳Ａ股\t00101106569\t200\t200\t\t\n",
      "\t000651\t格力电器\t1100\t1100\t0\t-2319.30\t47.958\t-4.40\t45.85\t50435.00\t深圳Ａ股\t00101106569\t1100\t1100\t\t\n",
      "\t162411\t华宝油气\t1000\t800\t200\t-116.62\t0.618\t-18.88\t0.50\t501.00\t深圳Ａ股\t00101106569\t1000\t800\t\t\n",
      "\t600048\t保利地产\t3800\t3800\t0\t-1955.94\t13.735\t-3.75\t13.22\t50236.00\t上海Ａ股\tA476826692\t3800\t3800\t\t\n",
      "\t601155\t新城控股\t1600\t1600\t0\t13465.48\t30.634\t27.47\t39.05\t62480.00\t上海Ａ股\tA476826692\t1600\t1600\t\t\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\n"
     ]
    }
   ],
   "source": [
    "grid = main.window(control_id=0x417, class_name='CVirtualGridCtrl')\n",
    "\n",
    "grid.set_focus().right_click()   # 模拟右键\n",
    "pywinauto.keyboard.SendKeys('c') # 模拟发送C\n",
    "\n",
    "pywinauto.keyboard.SendKeys('{DOWN}')\n",
    "time.sleep(0.1)\n",
    "pywinauto.keyboard.SendKeys('{DOWN}')\n",
    "time.sleep(0.1)\n",
    "pywinauto.keyboard.SendKeys('{DOWN}')\n",
    "time.sleep(0.1)\n",
    "pywinauto.keyboard.SendKeys(\"{ENTER}\")\n",
    "\n",
    "file_path = \"tmp.png\"\n",
    "\n",
    "while True:\n",
    "    app.top_window().window(\n",
    "        control_id=0x965,\n",
    "        class_name='Static'\n",
    "    ).CaptureAsImage().save(file_path)  #保存验证码\n",
    "\n",
    "    captcha_num = captcha_recognize(file_path)  # 识别验证码\n",
    "\n",
    "    app.top_window().window(\n",
    "        control_id=0x964,\n",
    "        class_name='Edit'\n",
    "    ).set_text(captcha_num)  # 模拟输入验证码\n",
    "\n",
    "    app.top_window().set_focus()\n",
    "    pywinauto.keyboard.SendKeys(\"{ENTER}\")   # 模拟发送enter，点击确定\n",
    "\n",
    "    try:\n",
    "        print(app.top_window().window(control_id=0x966, class_name='Static').window_text())\n",
    "    except:\n",
    "        break\n",
    "    time.sleep(0.1)\n",
    "\n",
    "print(clipboard.GetData())  # "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import io"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>明细</th>\n",
       "      <th>证券代码</th>\n",
       "      <th>证券名称</th>\n",
       "      <th>股票余额</th>\n",
       "      <th>可用余额</th>\n",
       "      <th>冻结数量</th>\n",
       "      <th>盈亏</th>\n",
       "      <th>成本价</th>\n",
       "      <th>盈亏比例(%)</th>\n",
       "      <th>市价</th>\n",
       "      <th>市值</th>\n",
       "      <th>交易市场</th>\n",
       "      <th>股东帐户</th>\n",
       "      <th>实际数量</th>\n",
       "      <th>可申赎数量</th>\n",
       "      <th>资讯</th>\n",
       "      <th>Unnamed: 16</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td></td>\n",
       "      <td>333</td>\n",
       "      <td>美的集团</td>\n",
       "      <td>200</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "      <td>5623.08</td>\n",
       "      <td>18.985</td>\n",
       "      <td>148.10</td>\n",
       "      <td>47.10</td>\n",
       "      <td>9420.0</td>\n",
       "      <td>深圳Ａ股</td>\n",
       "      <td>00101106569</td>\n",
       "      <td>200</td>\n",
       "      <td>200</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td></td>\n",
       "      <td>651</td>\n",
       "      <td>格力电器</td>\n",
       "      <td>1100</td>\n",
       "      <td>1100</td>\n",
       "      <td>0</td>\n",
       "      <td>-2319.30</td>\n",
       "      <td>47.958</td>\n",
       "      <td>-4.40</td>\n",
       "      <td>45.85</td>\n",
       "      <td>50435.0</td>\n",
       "      <td>深圳Ａ股</td>\n",
       "      <td>00101106569</td>\n",
       "      <td>1100</td>\n",
       "      <td>1100</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td></td>\n",
       "      <td>162411</td>\n",
       "      <td>华宝油气</td>\n",
       "      <td>1000</td>\n",
       "      <td>800</td>\n",
       "      <td>200</td>\n",
       "      <td>-116.62</td>\n",
       "      <td>0.618</td>\n",
       "      <td>-18.88</td>\n",
       "      <td>0.50</td>\n",
       "      <td>501.0</td>\n",
       "      <td>深圳Ａ股</td>\n",
       "      <td>00101106569</td>\n",
       "      <td>1000</td>\n",
       "      <td>800</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td></td>\n",
       "      <td>600048</td>\n",
       "      <td>保利地产</td>\n",
       "      <td>3800</td>\n",
       "      <td>3800</td>\n",
       "      <td>0</td>\n",
       "      <td>-1955.94</td>\n",
       "      <td>13.735</td>\n",
       "      <td>-3.75</td>\n",
       "      <td>13.22</td>\n",
       "      <td>50236.0</td>\n",
       "      <td>上海Ａ股</td>\n",
       "      <td>A476826692</td>\n",
       "      <td>3800</td>\n",
       "      <td>3800</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td></td>\n",
       "      <td>601155</td>\n",
       "      <td>新城控股</td>\n",
       "      <td>1600</td>\n",
       "      <td>1600</td>\n",
       "      <td>0</td>\n",
       "      <td>13465.48</td>\n",
       "      <td>30.634</td>\n",
       "      <td>27.47</td>\n",
       "      <td>39.05</td>\n",
       "      <td>62480.0</td>\n",
       "      <td>上海Ａ股</td>\n",
       "      <td>A476826692</td>\n",
       "      <td>1600</td>\n",
       "      <td>1600</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  明细    证券代码  证券名称  股票余额  可用余额  冻结数量        盈亏     成本价  盈亏比例(%)     市价  \\\n",
       "0        333  美的集团   200   200     0   5623.08  18.985   148.10  47.10   \n",
       "1        651  格力电器  1100  1100     0  -2319.30  47.958    -4.40  45.85   \n",
       "2     162411  华宝油气  1000   800   200   -116.62   0.618   -18.88   0.50   \n",
       "3     600048  保利地产  3800  3800     0  -1955.94  13.735    -3.75  13.22   \n",
       "4     601155  新城控股  1600  1600     0  13465.48  30.634    27.47  39.05   \n",
       "\n",
       "        市值  交易市场         股东帐户  实际数量  可申赎数量 资讯 Unnamed: 16  \n",
       "0   9420.0  深圳Ａ股  00101106569   200    200                 \n",
       "1  50435.0  深圳Ａ股  00101106569  1100   1100                 \n",
       "2    501.0  深圳Ａ股  00101106569  1000    800                 \n",
       "3  50236.0  上海Ａ股   A476826692  3800   3800                 \n",
       "4  62480.0  上海Ａ股   A476826692  1600   1600                 "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = clipboard.GetData()\n",
    "df = pd.read_csv(io.StringIO(data),\n",
    "                 delimiter='\\t',\n",
    "                 na_filter=False)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模拟买入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pywinauto.controls.common_controls._treeview_element at 0x191bf9d80b8>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_left_menus_handle().get_item(['买入[F1]']).click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_id = \"162411\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "您的买入委托已成功提交，合同编号：1025482214。\n"
     ]
    }
   ],
   "source": [
    "main.window(control_id=0x408, class_name=\"Edit\").set_text(stock_id)  # 设置股票代码\n",
    "\n",
    "main.window(control_id=0x409, class_name=\"Edit\").set_text(\"0.492\")  # 设置价格\n",
    "\n",
    "main.window(control_id=0x40A, class_name=\"Edit\").set_text(\"100\")   # 设置股数目\n",
    "\n",
    "main.window(control_id=0x3EE, class_name=\"Button\").click()   # 点击买入\n",
    "\n",
    "app.top_window().window(control_id=0x6, class_name='Button').click()  # 确定买入\n",
    "app.top_window().set_focus()\n",
    "print(app.top_window().window(control_id=0x3EC, class_name='Static').window_text())\n",
    "app.top_window().set_focus()\n",
    "app.top_window().window(control_id=0x2, class_name='Button').click()  # 确定"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模拟买出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pywinauto.controls.common_controls._treeview_element at 0x191c478f668>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_left_menus_handle().get_item(['卖出[F2]']).click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "您的卖出委托已成功提交，合同编号：1025476425。\n"
     ]
    }
   ],
   "source": [
    "main.window(control_id=0x408, class_name=\"Edit\").set_text(stock_id)  # 设置股票代码\n",
    "\n",
    "main.window(control_id=0x409, class_name=\"Edit\").set_text(\"0.510\")  # 设置价格\n",
    "\n",
    "main.window(control_id=0x40A, class_name=\"Edit\").set_text(\"100\")   # 设置股数目\n",
    "\n",
    "main.window(control_id=0x3EE, class_name=\"Button\").click()   # 点击卖出\n",
    "\n",
    "app.top_window().window(control_id=0x6, class_name='Button').click()  # 确定卖出\n",
    "app.top_window().set_focus()\n",
    "print(app.top_window().window(control_id=0x3EC, class_name='Static').window_text())\n",
    "app.top_window().set_focus()\n",
    "app.top_window().window(control_id=0x2, class_name='Button').click()  # 确定"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取当日委托"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pywinauto.controls.common_controls._treeview_element at 0x191c47972b0>"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_left_menus_handle().get_item(['查询[F4]', '当日委托']).click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "print(app.top_window().window(control_id=0x966, class_name='Static').window_text())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>备注</th>\n",
       "      <th>操作</th>\n",
       "      <th>证券代码</th>\n",
       "      <th>证券名称</th>\n",
       "      <th>委托数量</th>\n",
       "      <th>成交数量</th>\n",
       "      <th>委托价格</th>\n",
       "      <th>成交均价</th>\n",
       "      <th>撤消数量</th>\n",
       "      <th>委托时间</th>\n",
       "      <th>委托日期</th>\n",
       "      <th>合同编号</th>\n",
       "      <th>Unnamed: 12</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>未成交</td>\n",
       "      <td>卖出</td>\n",
       "      <td>162411</td>\n",
       "      <td>华宝油气</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "      <td>0.510</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "      <td>10:33:28</td>\n",
       "      <td>20190314</td>\n",
       "      <td>1025353126</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>未成交</td>\n",
       "      <td>卖出</td>\n",
       "      <td>162411</td>\n",
       "      <td>华宝油气</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "      <td>0.510</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "      <td>10:32:46</td>\n",
       "      <td>20190314</td>\n",
       "      <td>1025366444</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>全部成交</td>\n",
       "      <td>买入</td>\n",
       "      <td>162411</td>\n",
       "      <td>华宝油气</td>\n",
       "      <td>100</td>\n",
       "      <td>100</td>\n",
       "      <td>0.501</td>\n",
       "      <td>0.500</td>\n",
       "      <td>0</td>\n",
       "      <td>10:56:40</td>\n",
       "      <td>20190314</td>\n",
       "      <td>1025459094</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>全部成交</td>\n",
       "      <td>买入</td>\n",
       "      <td>162411</td>\n",
       "      <td>华宝油气</td>\n",
       "      <td>100</td>\n",
       "      <td>100</td>\n",
       "      <td>0.501</td>\n",
       "      <td>0.501</td>\n",
       "      <td>0</td>\n",
       "      <td>10:59:10</td>\n",
       "      <td>20190314</td>\n",
       "      <td>1025464948</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>全部成交</td>\n",
       "      <td>买入</td>\n",
       "      <td>162411</td>\n",
       "      <td>华宝油气</td>\n",
       "      <td>100</td>\n",
       "      <td>100</td>\n",
       "      <td>0.501</td>\n",
       "      <td>0.500</td>\n",
       "      <td>0</td>\n",
       "      <td>11:00:59</td>\n",
       "      <td>20190314</td>\n",
       "      <td>1025470854</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>未成交</td>\n",
       "      <td>卖出</td>\n",
       "      <td>162411</td>\n",
       "      <td>华宝油气</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "      <td>0.510</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "      <td>11:05:45</td>\n",
       "      <td>20190314</td>\n",
       "      <td>1025471336</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>未成交</td>\n",
       "      <td>卖出</td>\n",
       "      <td>162411</td>\n",
       "      <td>华宝油气</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "      <td>0.510</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "      <td>11:06:24</td>\n",
       "      <td>20190314</td>\n",
       "      <td>1025476425</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>未成交</td>\n",
       "      <td>买入</td>\n",
       "      <td>162411</td>\n",
       "      <td>华宝油气</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "      <td>0.492</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "      <td>11:05:28</td>\n",
       "      <td>20190314</td>\n",
       "      <td>1025482214</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     备注  操作    证券代码  证券名称  委托数量  成交数量   委托价格   成交均价  撤消数量      委托时间      委托日期  \\\n",
       "0   未成交  卖出  162411  华宝油气   100     0  0.510  0.000     0  10:33:28  20190314   \n",
       "1   未成交  卖出  162411  华宝油气   100     0  0.510  0.000     0  10:32:46  20190314   \n",
       "2  全部成交  买入  162411  华宝油气   100   100  0.501  0.500     0  10:56:40  20190314   \n",
       "3  全部成交  买入  162411  华宝油气   100   100  0.501  0.501     0  10:59:10  20190314   \n",
       "4  全部成交  买入  162411  华宝油气   100   100  0.501  0.500     0  11:00:59  20190314   \n",
       "5   未成交  卖出  162411  华宝油气   100     0  0.510  0.000     0  11:05:45  20190314   \n",
       "6   未成交  卖出  162411  华宝油气   100     0  0.510  0.000     0  11:06:24  20190314   \n",
       "7   未成交  买入  162411  华宝油气   100     0  0.492  0.000     0  11:05:28  20190314   \n",
       "\n",
       "         合同编号 Unnamed: 12  \n",
       "0  1025353126              \n",
       "1  1025366444              \n",
       "2  1025459094              \n",
       "3  1025464948              \n",
       "4  1025470854              \n",
       "5  1025471336              \n",
       "6  1025476425              \n",
       "7  1025482214              "
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid = main.window(\n",
    "    control_id=0x417,\n",
    "    class_name='CVirtualGridCtrl'\n",
    ")\n",
    "\n",
    "grid.set_focus().right_click()   # 模拟右键\n",
    "pywinauto.keyboard.SendKeys('{DOWN}')\n",
    "time.sleep(0.1)\n",
    "pywinauto.keyboard.SendKeys('{DOWN}')\n",
    "time.sleep(0.1)\n",
    "pywinauto.keyboard.SendKeys('{DOWN}')\n",
    "time.sleep(0.1)\n",
    "pywinauto.keyboard.SendKeys(\"{ENTER}\")\n",
    "\n",
    "file_path = \"tmp.png\"\n",
    "while True:\n",
    "    app.top_window().window(\n",
    "        control_id=0x965,\n",
    "        class_name='Static'\n",
    "    ).CaptureAsImage().save(file_path)  #保存验证码\n",
    "\n",
    "    captcha_num = captcha_recognize(file_path)  # 识别验证码\n",
    "\n",
    "    app.top_window().window(\n",
    "        control_id=0x964,\n",
    "        class_name='Edit'\n",
    "    ).set_text(captcha_num)  # 模拟输入验证码\n",
    "\n",
    "    app.top_window().set_focus()\n",
    "    pywinauto.keyboard.SendKeys(\"{ENTER}\")   # 模拟发送enter，点击确定\n",
    "\n",
    "    try:\n",
    "        print(app.top_window().window(control_id=0x966, class_name='Static').window_text())\n",
    "    except:\n",
    "        break\n",
    "    time.sleep(0.1)\n",
    "    \n",
    "data = clipboard.GetData()\n",
    "df = pd.read_csv(io.StringIO(data),\n",
    "                 delimiter='\\t',\n",
    "                 na_filter=False)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取当日成交"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pywinauto.controls.common_controls._treeview_element at 0x191c469d0f0>"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_left_menus_handle().get_item(['查询[F4]', '当日成交']).click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>成交时间</th>\n",
       "      <th>证券代码</th>\n",
       "      <th>证券名称</th>\n",
       "      <th>操作</th>\n",
       "      <th>成交数量</th>\n",
       "      <th>成交均价</th>\n",
       "      <th>成交金额</th>\n",
       "      <th>合同编号</th>\n",
       "      <th>成交编号</th>\n",
       "      <th>撤单数量</th>\n",
       "      <th>股票余额</th>\n",
       "      <th>Unnamed: 11</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10:56:43</td>\n",
       "      <td>162411</td>\n",
       "      <td>华宝油气</td>\n",
       "      <td>买入</td>\n",
       "      <td>100</td>\n",
       "      <td>0.500</td>\n",
       "      <td>50.0</td>\n",
       "      <td>1025459094</td>\n",
       "      <td>2615637099</td>\n",
       "      <td>0</td>\n",
       "      <td>1100</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10:59:12</td>\n",
       "      <td>162411</td>\n",
       "      <td>华宝油气</td>\n",
       "      <td>买入</td>\n",
       "      <td>100</td>\n",
       "      <td>0.501</td>\n",
       "      <td>50.1</td>\n",
       "      <td>1025464948</td>\n",
       "      <td>2615645434</td>\n",
       "      <td>0</td>\n",
       "      <td>1200</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>11:01:06</td>\n",
       "      <td>162411</td>\n",
       "      <td>华宝油气</td>\n",
       "      <td>买入</td>\n",
       "      <td>100</td>\n",
       "      <td>0.500</td>\n",
       "      <td>50.0</td>\n",
       "      <td>1025470854</td>\n",
       "      <td>2615651632</td>\n",
       "      <td>0</td>\n",
       "      <td>1300</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       成交时间    证券代码  证券名称  操作  成交数量   成交均价  成交金额        合同编号        成交编号  \\\n",
       "0  10:56:43  162411  华宝油气  买入   100  0.500  50.0  1025459094  2615637099   \n",
       "1  10:59:12  162411  华宝油气  买入   100  0.501  50.1  1025464948  2615645434   \n",
       "2  11:01:06  162411  华宝油气  买入   100  0.500  50.0  1025470854  2615651632   \n",
       "\n",
       "   撤单数量  股票余额 Unnamed: 11  \n",
       "0     0  1100              \n",
       "1     0  1200              \n",
       "2     0  1300              "
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid = main.window(\n",
    "    control_id=0x417,\n",
    "    class_name='CVirtualGridCtrl'\n",
    ")\n",
    "grid.set_focus().right_click()   # 模拟右键\n",
    "pywinauto.keyboard.SendKeys('{DOWN}')\n",
    "time.sleep(0.1)\n",
    "pywinauto.keyboard.SendKeys('{DOWN}')\n",
    "time.sleep(0.1)\n",
    "pywinauto.keyboard.SendKeys('{DOWN}')\n",
    "time.sleep(0.1)\n",
    "pywinauto.keyboard.SendKeys(\"{ENTER}\")\n",
    "\n",
    "\n",
    "file_path = \"tmp.png\"\n",
    "while True:\n",
    "    app.top_window().window(\n",
    "        control_id=0x965,\n",
    "        class_name='Static'\n",
    "    ).CaptureAsImage().save(file_path)  #保存验证码\n",
    "\n",
    "    captcha_num = captcha_recognize(file_path)  # 识别验证码\n",
    "\n",
    "    app.top_window().window(\n",
    "        control_id=0x964,\n",
    "        class_name='Edit'\n",
    "    ).set_text(captcha_num)  # 模拟输入验证码\n",
    "\n",
    "    app.top_window().set_focus()\n",
    "    pywinauto.keyboard.SendKeys(\"{ENTER}\")   # 模拟发送enter，点击确定\n",
    "\n",
    "    try:\n",
    "        print(app.top_window().window(control_id=0x966, class_name='Static').window_text())\n",
    "    except:\n",
    "        break\n",
    "    time.sleep(0.1)\n",
    "\n",
    "# print(clipboard.GetData())\n",
    "\n",
    "data = clipboard.GetData()\n",
    "df = pd.read_csv(io.StringIO(data),\n",
    "                 delimiter='\\t',\n",
    "                 na_filter=False)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 撤单"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pywinauto.controls.common_controls._treeview_element at 0x191c4747b38>"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_left_menus_handle().get_item(['撤单[F3]']).click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "验证码错误！！\n",
      "委托时间\t证券代码\t证券名称\t操作\t备注\t委托数量\t成交数量\t撤消数量\t委托价格\t订单类型\t成交均价\t合同编号\t\n",
      "10:33:28\t162411\t华宝油气\t卖出\t未成交\t100\t0\t0\t0.510\t限价\t0.000\t1025353126\t\n",
      "10:32:46\t162411\t华宝油气\t卖出\t未成交\t100\t0\t0\t0.510\t限价\t0.000\t1025366444\t\n",
      "11:05:45\t162411\t华宝油气\t卖出\t未成交\t100\t0\t0\t0.510\t限价\t0.000\t1025471336\t\n",
      "11:06:24\t162411\t华宝油气\t卖出\t未成交\t100\t0\t0\t0.510\t限价\t0.000\t1025476425\t\n",
      "11:05:28\t162411\t华宝油气\t买入\t未成交\t100\t0\t0\t0.492\t限价\t0.000\t1025482214\t\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\n"
     ]
    }
   ],
   "source": [
    "def get_entrusts():\n",
    "    grid = main.window(\n",
    "        control_id=0x417,\n",
    "        class_name='CVirtualGridCtrl'\n",
    "    )\n",
    "    grid.set_focus().right_click()   # 模拟右键\n",
    "    pywinauto.keyboard.SendKeys('{DOWN}')\n",
    "    time.sleep(0.1)\n",
    "    pywinauto.keyboard.SendKeys('{DOWN}')\n",
    "    time.sleep(0.1)\n",
    "    pywinauto.keyboard.SendKeys(\"{ENTER}\")\n",
    "\n",
    "\n",
    "    file_path = \"tmp.png\"\n",
    "    while True:\n",
    "        app.top_window().window(\n",
    "            control_id=0x965,\n",
    "            class_name='Static'\n",
    "        ).CaptureAsImage().save(file_path)  #保存验证码\n",
    "\n",
    "        captcha_num = captcha_recognize(file_path)  # 识别验证码\n",
    "\n",
    "        app.top_window().window(\n",
    "            control_id=0x964,\n",
    "            class_name='Edit'\n",
    "        ).set_text(captcha_num)  # 模拟输入验证码\n",
    "\n",
    "        app.top_window().set_focus()\n",
    "        pywinauto.keyboard.SendKeys(\"{ENTER}\")   # 模拟发送enter，点击确定\n",
    "\n",
    "        try:\n",
    "            print(app.top_window().window(control_id=0x966, class_name='Static').window_text())\n",
    "        except:\n",
    "            break\n",
    "        time.sleep(0.1)\n",
    "\n",
    "    print(clipboard.GetData())\n",
    "\n",
    "    data = clipboard.GetData()\n",
    "    df = pd.read_csv(io.StringIO(data),\n",
    "                     delimiter='\\t',\n",
    "                     na_filter=False)\n",
    "    return df.to_dict('records')\n",
    "\n",
    "entrusts = get_entrusts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'委托时间': '10:33:28',\n",
       "  '证券代码': 162411,\n",
       "  '证券名称': '华宝油气',\n",
       "  '操作': '卖出',\n",
       "  '备注': '未成交',\n",
       "  '委托数量': 100,\n",
       "  '成交数量': 0,\n",
       "  '撤消数量': 0,\n",
       "  '委托价格': 0.51,\n",
       "  '订单类型': '限价',\n",
       "  '成交均价': 0.0,\n",
       "  '合同编号': 1025353126,\n",
       "  'Unnamed: 12': ''},\n",
       " {'委托时间': '10:32:46',\n",
       "  '证券代码': 162411,\n",
       "  '证券名称': '华宝油气',\n",
       "  '操作': '卖出',\n",
       "  '备注': '未成交',\n",
       "  '委托数量': 100,\n",
       "  '成交数量': 0,\n",
       "  '撤消数量': 0,\n",
       "  '委托价格': 0.51,\n",
       "  '订单类型': '限价',\n",
       "  '成交均价': 0.0,\n",
       "  '合同编号': 1025366444,\n",
       "  'Unnamed: 12': ''},\n",
       " {'委托时间': '11:05:45',\n",
       "  '证券代码': 162411,\n",
       "  '证券名称': '华宝油气',\n",
       "  '操作': '卖出',\n",
       "  '备注': '未成交',\n",
       "  '委托数量': 100,\n",
       "  '成交数量': 0,\n",
       "  '撤消数量': 0,\n",
       "  '委托价格': 0.51,\n",
       "  '订单类型': '限价',\n",
       "  '成交均价': 0.0,\n",
       "  '合同编号': 1025471336,\n",
       "  'Unnamed: 12': ''},\n",
       " {'委托时间': '11:06:24',\n",
       "  '证券代码': 162411,\n",
       "  '证券名称': '华宝油气',\n",
       "  '操作': '卖出',\n",
       "  '备注': '未成交',\n",
       "  '委托数量': 100,\n",
       "  '成交数量': 0,\n",
       "  '撤消数量': 0,\n",
       "  '委托价格': 0.51,\n",
       "  '订单类型': '限价',\n",
       "  '成交均价': 0.0,\n",
       "  '合同编号': 1025476425,\n",
       "  'Unnamed: 12': ''},\n",
       " {'委托时间': '11:05:28',\n",
       "  '证券代码': 162411,\n",
       "  '证券名称': '华宝油气',\n",
       "  '操作': '买入',\n",
       "  '备注': '未成交',\n",
       "  '委托数量': 100,\n",
       "  '成交数量': 0,\n",
       "  '撤消数量': 0,\n",
       "  '委托价格': 0.49200000000000005,\n",
       "  '订单类型': '限价',\n",
       "  '成交均价': 0.0,\n",
       "  '合同编号': 1025482214,\n",
       "  'Unnamed: 12': ''}]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "entrusts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "entrust_no = \"1025353126\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cancel_by_double_click(row):\n",
    "    app.top_window().set_focus()\n",
    "    x = 50\n",
    "    y = 30 + 16 * row\n",
    "    app.top_window().window(\n",
    "            control_id=1047,\n",
    "            class_name='CVirtualGridCtrl'\n",
    "    ).double_click(coords=(x, y))\n",
    "    \n",
    "    app.top_window().window(control_id=0x6, class_name='Button').click()  # 确定撤单"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1025353126\n",
      "canceling 1025353126\n"
     ]
    },
    {
     "ename": "ElementNotFoundError",
     "evalue": "{'control_id': 6, 'class_name': 'Button', 'top_level_only': False, 'parent': <win32_element_info.HwndElementInfo - '网上股票交易系统5.0', Afx:400000:b:10003:6:20f10b9b, 199738>, 'backend': 'win32'}",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTimeoutError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pywinauto\\application.py\u001b[0m in \u001b[0;36m__resolve_control\u001b[1;34m(self, criteria, timeout, retry_interval)\u001b[0m\n\u001b[0;32m    244\u001b[0m                  controls.InvalidElement),\n\u001b[1;32m--> 245\u001b[1;33m                 criteria)\n\u001b[0m\u001b[0;32m    246\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pywinauto\\timings.py\u001b[0m in \u001b[0;36mwait_until_passes\u001b[1;34m(timeout, retry_interval, func, exceptions, *args)\u001b[0m\n\u001b[0;32m    448\u001b[0m                 \u001b[0merr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0moriginal_exception\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 449\u001b[1;33m                 \u001b[1;32mraise\u001b[0m \u001b[0merr\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    450\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTimeoutError\u001b[0m: ",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mElementNotFoundError\u001b[0m                      Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-72-fddc62db9a4e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mentrust\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"合同编号\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mentrust_no\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"canceling\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mentrust_no\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m         \u001b[0mcancel_by_double_click\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-71-a246eefce98e>\u001b[0m in \u001b[0;36mcancel_by_double_click\u001b[1;34m(row)\u001b[0m\n\u001b[0;32m      8\u001b[0m     ).double_click(coords=(x, y))\n\u001b[0;32m      9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m     \u001b[0mapp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtop_window\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwindow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcontrol_id\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0x6\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mclass_name\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'Button'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclick\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m# 确定撤单\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pywinauto\\application.py\u001b[0m in \u001b[0;36m__getattribute__\u001b[1;34m(self, attr_name)\u001b[0m\n\u001b[0;32m    349\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcriteria\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m>=\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    350\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 351\u001b[1;33m             \u001b[0mctrls\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__resolve_control\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcriteria\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    352\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    353\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pywinauto\\application.py\u001b[0m in \u001b[0;36m__resolve_control\u001b[1;34m(self, criteria, timeout, retry_interval)\u001b[0m\n\u001b[0;32m    246\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    247\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mTimeoutError\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 248\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0moriginal_exception\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    249\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    250\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mctrl\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pywinauto\\timings.py\u001b[0m in \u001b[0;36mwait_until_passes\u001b[1;34m(timeout, retry_interval, func, exceptions, *args)\u001b[0m\n\u001b[0;32m    425\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    426\u001b[0m             \u001b[1;31m# Call the function with any arguments\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 427\u001b[1;33m             \u001b[0mfunc_val\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    428\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    429\u001b[0m             \u001b[1;31m# if no exception is raised then we are finished\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pywinauto\\application.py\u001b[0m in \u001b[0;36m__get_ctrl\u001b[1;34m(self, criteria_)\u001b[0m\n\u001b[0;32m    207\u001b[0m                 \u001b[1;32mif\u001b[0m \u001b[1;34m'backend'\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mctrl_criteria\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    208\u001b[0m                     \u001b[0mctrl_criteria\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'backend'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbackend\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 209\u001b[1;33m                 \u001b[0mctrl\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbackend\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgeneric_wrapper_class\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfindwindows\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfind_element\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mctrl_criteria\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    210\u001b[0m                 \u001b[0mprevious_parent\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mctrl\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0melement_info\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    211\u001b[0m                 \u001b[0mctrls\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mctrl\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pywinauto\\findwindows.py\u001b[0m in \u001b[0;36mfind_element\u001b[1;34m(**kwargs)\u001b[0m\n\u001b[0;32m     85\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     86\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0melements\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 87\u001b[1;33m         \u001b[1;32mraise\u001b[0m \u001b[0mElementNotFoundError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     88\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     89\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0melements\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mElementNotFoundError\u001b[0m: {'control_id': 6, 'class_name': 'Button', 'top_level_only': False, 'parent': <win32_element_info.HwndElementInfo - '网上股票交易系统5.0', Afx:400000:b:10003:6:20f10b9b, 199738>, 'backend': 'win32'}"
     ]
    }
   ],
   "source": [
    "for i, entrust in enumerate(entrusts):\n",
    "    print(i, entrust[\"合同编号\"])\n",
    "    if str(entrust[\"合同编号\"]) == entrust_no:\n",
    "        print(\"canceling\", entrust_no)\n",
    "        cancel_by_double_click(i)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
